package io.github.noahzu.daka

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.ActionBar
import cn.leancloud.AVObject
import com.blankj.utilcode.util.ToastUtils
import io.github.noahzu.daka.bean.ConstructionBean
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_add_project.*
import java.util.*

class AddProjectActivity : BaseActivity() {
    override fun layoutId(): Int = R.layout.activity_add_project

    override fun beforeCreate() {
        val actionBar: ActionBar? = supportActionBar
        actionBar?.hide()
    }

    override fun afterCreate() {
        mSubmitBtn.setOnClickListener {
            doSubmit()
        }
    }

    private fun doSubmit() {
        val projectName = mProjectNameEdit.text.toString()
        val constructionUnit = mConstructionUnitEdit.text.toString()
        val projectLocation  = mProjectlocationEdit.text.toString()
        val remarks = mRemarksEdit.text.toString()
        val sponsor = mSponsorEdit.text.toString()
        val verbalWar = mVerbalWarningEdit.text.toString()
        val personInChar = mPersonInChargeEdit.text.toString()
        val notes = mNotesEdit.text.toString()

        if(projectName.isNullOrEmpty()) {
            ToastUtils.showShort("项目名称不能为空")
            return
        }
        if(constructionUnit.isNullOrEmpty()) {
            ToastUtils.showShort("施工单位不能为空")
            return
        }
        if(sponsor.isNullOrEmpty()) {
            ToastUtils.showShort("责任人不能为空")
            return
        }
        if(personInChar.isNullOrEmpty()) {
            ToastUtils.showShort("主办人不能为空")
            return
        }

        val avObject = AVObject("ConstructionBean")
        avObject.put("ConstructionUnit",constructionUnit)
        avObject.put("Notes",notes)
        avObject.put("ProjectName",projectName)
        avObject.put("Projectlocation",projectLocation)
        avObject.put("Remarks",remarks)
        avObject.put("Sponsor",sponsor)
        avObject.put("Time",Date())
        avObject.put("VerbalWarning",verbalWar)
        avObject.put("personInCharge",personInChar)

        mProgressDialog.visibility = View.VISIBLE
        avObject.saveInBackground()
            .subscribeOn(Schedulers.io())//这里指定在io线程执行
            .observeOn(AndroidSchedulers.mainThread())//返回结果在主线程执行
            .subscribe(object : Observer<AVObject>{
                override fun onComplete() = Unit

                override fun onSubscribe(d: Disposable) = Unit

                override fun onNext(t: AVObject) {
                    ToastUtils.showLong("提交成功")
                    mProgressDialog.visibility = View.GONE
                    setResult(Activity.RESULT_OK)
                    finish()
                }

                override fun onError(e: Throwable) {
                    ToastUtils.showLong("提交失败")
                    mProgressDialog.visibility = View.GONE
                }

            })
    }

}
